package edu.software.PBL_Management_System.demandhistory.service;

import edu.software.PBL_Management_System.demandhistory.repository.DemandHistoryRepository;
import edu.software.PBL_Management_System.entity.Demand;
import edu.software.PBL_Management_System.entity.DemandHistory;
import edu.software.PBL_Management_System.entity.User;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DemandHistoryService {

    //依赖注入
    @Resource
    private DemandHistoryRepository demandHistoryRepository;

    /**
     * 创建新的需求历史记录
     */
    @Transactional
    public DemandHistory createHistory(DemandHistory demandHistory) {
        return demandHistoryRepository.save(demandHistory);
    }

    /**
     * 记录脑图更新历史
     */
    @Transactional
    public DemandHistory recordMindMapUpdate(Demand demand, String oldMindMapData, String newMindMapData, User modifier) {
        DemandHistory history = new DemandHistory();
        history.setDemandId(demand.getId());
        history.setModifierId(modifier.getId());
        history.setModifierName(modifier.getUsername());
        history.setOriginalMindmap(oldMindMapData);
        history.setModifiedMindmap(newMindMapData);
        history.setModificationType(DemandHistory.ModificationType.MINDMAP_UPDATE);
        return createHistory(history);
    }

    /**
     * 记录描述更新历史
     */
    @Transactional
    public DemandHistory recordDescriptionUpdate(Demand demand, String oldDescription, String newDescription, User modifier) {
        DemandHistory history = new DemandHistory();
        history.setDemandId(demand.getId());
        history.setModifierId(modifier.getId());
        history.setModifierName(modifier.getUsername());
        history.setOriginalContent(oldDescription);
        history.setModifiedContent(newDescription);
        history.setModificationType(DemandHistory.ModificationType.DESCRIPTION_UPDATE);
        return createHistory(history);
    }

    /**
     * 记录脑图和描述同时更新的历史
     */
    @Transactional
    public DemandHistory recordBothUpdate(
            Demand demand,
            String oldDescription,
            String newDescription,
            String oldMindMapData,
            String newMindMapData,
            User modifier,
            String comment) {
        DemandHistory history = new DemandHistory();
        history.setDemandId(demand.getId());
        history.setModifierId(modifier.getId());
        // 使用传入的用户邮箱作为修改者名称，确保不为空
        String modifierEmail = modifier.getEmail();
        if (modifierEmail == null || modifierEmail.trim().isEmpty()) {
            modifierEmail = "未知用户";
        }
        history.setModifierName(modifierEmail);
        history.setOriginalContent(oldDescription);
        history.setModifiedContent(newDescription);
        history.setOriginalMindmap(oldMindMapData);
        history.setModifiedMindmap(newMindMapData);
        history.setModificationType(DemandHistory.ModificationType.BOTH_UPDATE);
        history.setComment(comment);
        return createHistory(history);
    }

    /**
     * 获取指定需求的所有历史记录
     */
    public List<DemandHistory> getDemandHistory(Long demandId) {
        return demandHistoryRepository.findByDemandIdOrderByModificationTimeDesc(demandId);
    }

    /**
     * 获取指定用户的修改历史
     */
    public List<DemandHistory> getUserModificationHistory(Long modifierId) {
        return demandHistoryRepository.findByModifierId(modifierId);
    }

    /**
     * 获取指定需求的特定类型的修改历史
     */
    public List<DemandHistory> getTypeSpecificHistory(Long demandId, DemandHistory.ModificationType modificationType) {
        return demandHistoryRepository.findByDemandIdAndModificationType(demandId, modificationType);
    }

    /**
     * 删除历史记录
     */
    @Transactional
    public void deleteHistory(Long historyId) {
        demandHistoryRepository.deleteById(historyId);
    }
}
